package com.wskj.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.wskj.common.core.page.PagePlus;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.system.bo.SysCommonAddBo;
import com.wskj.system.bo.SysCommonEditBo;
import com.wskj.system.bo.SysCommonQueryBo;
import com.wskj.system.domain.SysCommon;
import com.wskj.system.mapper.SysCommonMapper;
import com.wskj.system.service.ISysCommonService;
import com.wskj.system.vo.web.SysCommonVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 系统公共Service业务层处理
 *
 * @author wsdz
 * @date 2023-04-10
 */
@Service
public class SysCommonServiceImpl extends ServicePlusImpl<SysCommonMapper, SysCommon> implements ISysCommonService {

    @Override
    public SysCommonVo queryById(Long commonId){
        return getVoById(commonId, SysCommonVo.class);
    }

    @Override
    public TableDataInfo<SysCommonVo> queryPageList(SysCommonQueryBo bo) {
        PagePlus<SysCommon, SysCommonVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysCommonVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysCommonVo> queryList(SysCommonQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysCommonVo.class);
    }

    private LambdaQueryWrapper<SysCommon> buildQueryWrapper(SysCommonQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysCommon> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getCommonClass()), SysCommon::getCommonClass, bo.getCommonClass());
        lqw.like(StrUtil.isNotBlank(bo.getCommonName()), SysCommon::getCommonName, bo.getCommonName());
        lqw.eq(StrUtil.isNotBlank(bo.getCommonValue()), SysCommon::getCommonValue, bo.getCommonValue());
        lqw.eq(StrUtil.isNotBlank(bo.getCommonDesc()), SysCommon::getCommonDesc, bo.getCommonDesc());
        lqw.eq(bo.getSortNo() != null, SysCommon::getSortNo, bo.getSortNo());
        lqw.orderByAsc(SysCommon::getSortNo);
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysCommonAddBo bo) {
        SysCommon add = BeanUtil.toBean(bo, SysCommon.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysCommonEditBo bo) {
        SysCommon update = BeanUtil.toBean(bo, SysCommon.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysCommon entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importSysCommon(List<SysCommonVo> sysCommonList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(sysCommonList), "导入数据不能为空");
        int successNum = 0;
        for (SysCommonVo sysCommon : sysCommonList) {
            SysCommon add = BeanUtil.toBean(sysCommon, SysCommon.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", sysCommonList.size(), successNum);
        if (successNum != sysCommonList.size()) {
            msg = msg + String.format("失败 %d 条！", sysCommonList.size() - successNum);
        }
        return msg;
    }
}
